package fafmessenger_server.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import fafmessenger_server.control.Database;
import fafmessenger_server.entity.Message;
import fafmessenger_server.entity.User;

public class Messages extends ArrayList<Message> {
	
	private User user;
	private Database database;
	
	public Messages(User user) {
		super();
		
		this.user = user;
		database = Database.getRef();
		
		retrieve();
	}
	
	private boolean retrieve() {
		boolean returnvalue = false;
		
		if (database.connect()) {
			synchronized (this) {
				ResultSet result = database.executeQuery("SELECT * FROM message WHERE `to` = '" + user.getMobilenumber() + "';");
					
				if (result != null) {
					this.clear();
					
					try {
						while (result.next()) {
							Message message = new Message(result.getInt(1), result.getTimestamp("timestamp"), result.getString(3), result.getString(4), result.getString(5));
							this.add(message);
						}
						
						returnvalue = true;
						result.close();
						
					} catch (SQLException e) {
						e.printStackTrace();
					}				
				}
			}
			
			database.disconnect();
		}
	
		return returnvalue;
	}
	
	public boolean insert(Message message) {
		boolean returnvalue = false;
		
		if (database.connect()) {
			synchronized (this) {
				String query = 	"INSERT INTO message (`timestamp`, `body`, `from`, `to`)" +
								"VALUES ('" + message.getTimestamp() + "', '" + message.getBody() + "', '" + message.getFromMobilenumber() + "', '" + message.getToMobilenumber() + "');";
				returnvalue = database.executeUpdate(query);
				database.disconnect();
				
				if (returnvalue) {
					this.add(message);
				}
			}
		}
		
		return returnvalue;
	}
	
	public boolean insertAll(ArrayList<Message> messages) {
		boolean returnvalue = false;
		
		if (database.connect()) {
			synchronized (this) {
				for (Message m : messages) {
					String query = 	"INSERT INTO message (`timestamp`, `body`, `from`, `to`)" +
									"VALUES ('" + m.getTimestamp() + "', '" + m.getBody() + "', '" + m.getFromMobilenumber() + "', '" + m.getToMobilenumber() + "');";
					returnvalue = database.executeUpdate(query);
					
					if (returnvalue) {
						this.add(m);
					} else {
						break;
					}
				}
			}
			
			database.disconnect();
		}
		
		return returnvalue;
	}
	
	public boolean update(Message message) {
		boolean returnvalue = false;
		
		if (database.connect()) {		
			String query = 	"UPDATE message " +
							"SET `body` = '" + message.getBody() + "' " +
							"WHERE `id` = '" + message.getID() + "';";
			returnvalue = database.executeUpdate(query);
			database.disconnect();
		}
		
		return returnvalue;
	}
	
	public boolean updateAll() {
		boolean returnvalue = false;
		
		if (database.connect()) {
			for (Message m : this) {
				String query = 	"UPDATE message " +
								"SET `body` = '" + m.getBody() + "' " +
								"WHERE `id` = '" + m.getID() + "';";
				returnvalue = database.executeUpdate(query);
				
				if (!returnvalue) {
					break;
				}
			}
			
			database.disconnect();
		}
		
		return returnvalue;
	}
	
	public synchronized boolean delete(Message message) {
		boolean returnvalue = false;
		
		if (database.connect()) {
			synchronized (this) {
				String query = 	"DELETE FROM message " +
								"WHERE `id` = '" + message.getID() + "';";
				returnvalue = database.executeUpdate(query);
				database.disconnect();
				
				if (returnvalue) {
					this.remove(message);
				}
			}
		}
		
		return returnvalue;
	}
	
	public boolean deleteAll() {
		boolean returnvalue = false;
		
		if (database.connect()) {
			synchronized (this) {
				for (Message m : this) {
					String query = 	"DELETE FROM message " +
									"WHERE `id` = '" + m.getID() + "';";
					returnvalue = database.executeUpdate(query);
					database.disconnect();
					
					if (returnvalue) {
						this.remove(m);
					} else {
						break;
					}
				}
			}
		}
		
		return returnvalue;
	}

	public Message getByID(int id) {
		Message returnvalue = null;
		for (Message m : this) {
			if (m.getID() == id) {
				returnvalue = m;
				break;
			}
		}
		return returnvalue;
	}
	
	public ArrayList<Message> getByFrom(String fromMobilenumber) {
		ArrayList<Message> returnvalue = new ArrayList<Message>();
		for (Message m : this) {
			if (m.getFromMobilenumber().equals(fromMobilenumber)) {
				returnvalue.add(m);
			}
		}
		return returnvalue;
	}
}
